// pages/main/index.js
var QR = require("../../../utils/qrcode.js");
import {baseUrl} from '../../../configs/index.js'
Page({
    data: {
        canvasHidden: false,
        maskHidden: true,
        imagePath: '',
        user: wx.getStorageSync('user'),
    },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
     let user = wx.getStorageSync('user');
        var initUrl = `${baseUrl}?id=${user.userID}&form=invite`;
        console.log(initUrl);
        this.createQrCode(initUrl);
    },

    //适配不同屏幕大小的canvas
    setCanvasSize: function () {
        var size = {};
        try {
            var res = wx.getSystemInfoSync();
            var scale = 1;//不同屏幕下canvas的适配比例；设计稿是750宽
            var width = res.windowWidth / scale;
            var height = width;//canvas画布为正方形
            size.w = width;
            size.h = height;
        } catch (e) {
            // Do something when catch error
            console.log("获取设备信息失败" + e);
        }
        return size;
    },
    createQrCode: function (url) {
        var size = this.setCanvasSize();//动态设置画布大小
        //调用插件中的draw方法，绘制二维码图片
        QR.api.draw(url, "mycanvas", size.w, size.h);

    },
    //获取临时缓存照片路径，存入data中
    canvasToTempImage: function () {
        return new Promise((resolve, reject) => {
            wx.canvasToTempFilePath({
                canvasId: 'mycanvas',
                success: resolve,
                fail: reject
            });
        })

    },
    // 分享
    onShareAppMessage: function (res) {

       let {nickName,userID} = this.data.user;
        return {
            title: `${nickName}邀请你使用`,
            path: `/pages/index/index?&from=share&userId=${this.data.user.userID}&msg=个人邀请分享`,
            success: function (res) {
            },
            fail: function (res) {
                console.log('转发失败');
            }
        }
    },

})